package com.gitee.fanx.retty.spring;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.fanx.retty.web.Request;
import com.gitee.fanx.retty.spring.springmvc.HeaderParamResolver;
import com.gitee.fanx.retty.spring.springmvc.RequestBodyResolver;
import com.gitee.fanx.retty.spring.springmvc.RequestParamResolver;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author fanhang
 */
public interface ParamResolver {

	/**
	 * 获取参数值
	 * @param request 请求
	 * @return 请求参数值
	 */
	Object resolve(Request request);


	/**
	 * 创建 RequestParam 的 Resolver
	 * @param requestParam requestParam
	 * @param parameterName parameterName
	 * @param clazz clazz
	 * @param objectMapper objectMapper
	 * @return RequestParamResolver
	 */
	static ParamResolver requestParam(RequestParam requestParam, String parameterName, Class<?> clazz, ObjectMapper objectMapper) {
		return new RequestParamResolver(requestParam, parameterName, clazz, objectMapper);
	}

	/**
	 * 创建 RequestHeader 的 Resolver
	 * @param requestHeader requestHeader
	 * @param parameterName parameterName
	 * @param clazz clazz
	 * @param objectMapper objectMapper
	 * @return HeaderParamResolver
	 */
	static ParamResolver headerParam(RequestHeader requestHeader, String parameterName, Class<?> clazz, ObjectMapper objectMapper) {
		return new HeaderParamResolver(requestHeader, parameterName, clazz, objectMapper);
	}

	/**
	 * 创建 RequestBody 的 Resolver
	 * @param requestBody requestBody
	 * @param clazz clazz
	 * @param objectMapper objectMapper
	 * @return RequestBodyResolver
	 */
	static ParamResolver requestBody(RequestBody requestBody, Class<?> clazz, ObjectMapper objectMapper) {
		return new RequestBodyResolver(requestBody, clazz, objectMapper);
	}
}
